<?php
$title = "OnlineShop-Shopping Cart";
$css = "cart.css";
include_once 'common/top.php';
include_once '../Class/DB.php';
if(!$_SESSION["Passed"])
{
echo '<br><br><h3 align="center">You have not login.<h3>';
exit();
}
$db=new DB();
$discount_per_level=0.01;

$pids=$_GET["pids"];
if(isset($pids))
{
	$user=$_SESSION["User"];
	$pids=preg_split("/,/",$pids);
	$db->designList_to_cart($user,$discount_per_level,$pids);
	//print_r($pids);
	//exit();
?>
<script language="JavaScript">
window.location.href="cart.php";
</script>
<?php	
}
$flag=$_POST["flag"];
if($flag=="cart")
{
	$pCount=$_POST["number"];
	$pID=$_POST["item_pid"];
	$theProduct=$db->get_pid_product($pID);
	$should_pay=$theProduct[0]["act_price"]*$pCount*(1-$_SESSION["User"]["level"]*$discount_per_level);
	$should_pay=number_format($should_pay,1,'.','');
	//$should_pay=number_format($should_pay,2,'.','');
	$db->add_to_cart($_SESSION["User"]["uid"],$pID,$pCount,$should_pay);
}

$carts=$db->get_uid_carts($_SESSION["User"]["uid"]);
$encodes=json_encode($carts);//encode to javascript

if($flag=="save")
{
	$i=0;
while(isset($carts[$i]))
{
	$modifyCount=$_POST['count_name_'.$carts[$i]["oid"]];
	$modifyShould=$_POST['should_name_'.$carts[$i]["oid"]];
	$db->update_cart($carts[$i]["oid"], $modifyCount, $modifyShould,$carts[$i]["pid"],$carts[$i]["count"]);
	$i++;
}

?>
<script language="JavaScript">
window.location.href="cart.php";
</script>
<?php 
}

if($flag=="delete")
{

if($_POST["name_check_all"])//clean cart
{
$i=0;//order id
while(isset($carts[$i])){
		$db->delete_oid_order($carts[$i]["oid"],$carts[$i]["pid"],$carts[$i]["count"]);
		$i++;
}
}
else {//clean part of cart
$i=0;//order id
while(isset($carts[$i]))
{
	if($_POST["name_check_".$carts[$i]["oid"]])
	{
		$db->delete_oid_order($carts[$i]["oid"],$carts[$i]["pid"],$carts[$i]["count"]);
		
	}
	$i++;
}
}
?>
<script language="JavaScript">
window.location.href="cart.php";
</script>
<?php
}
if($flag=="pay")
{

}
?>
<script language="JavaScript">
var en_str='<?php print_r($encodes)?>';
var carts = JSON.parse(en_str);
</script>
<script src="../js/cart.js" type="text/javascript"></script>
<div id="cart">
	<?php 
	if(!isset($carts[0])){//if cart is empty
	?>
	<div id="empty">
		<h1>Your cart is empty.Let's go shopping now!</h1>
	</div>
	<?php 
	}else{
	?>
	<div id="products">
		<form action="cart.php" method="post" name="cartform">
		<input type="hidden" name="flag" id="id_flag"/>
		<?php 
		$i=0;
		$pids=array();
		while(isset($carts[$i]))
		{
			if(!in_array($carts[$i]["pid"], $pids))
			{
				$pids[]=$carts[$i]["pid"];
	          	$tempProduct=$db->get_pid_product($carts[$i]["pid"]);	
		?>
		<input type="hidden" name="left_name_<?php echo $carts[$i]["pid"];?>" id="left_<?php echo $carts[$i]["pid"];?>" value="<?php echo $tempProduct[0]["count"];?>"/>
		<?php 
		
			}
			$i++;
		}
		?>
		<input type="hidden" id="user_level" value="<?php echo $_SESSION['User']["level"];?>"/>
			<table>
				<tr class="tr0">
					<th>
						<input type="checkbox" name="name_check_all" id="checkbox_all" onclick="selectall(this)" />
						<p>Select All</p>
					</th>
					<th>
						<p>Name</p>
					</th>
					<th>
						<p>Point</p>
					</th>
					<th>
						<p>price($)</p>
					</th>
					<th>
						<p>count</p>
					</th>
					<th>
						<p>discount</p>
					</th>
					<th>
						<p>total</p>
					</th>
					<th>
						<p>operation</p>
					</th>
				</tr>
				<?php 
					$i=0;
				while(isset($carts[$i])){
					$A_product=$db->get_pid_product($carts[$i]["pid"]);
				?>
				<tr class="tr1">
					<td>
						<input type="checkbox" id="checkbox_<?php echo $carts[$i]["oid"];?>" name="name_check_<?php echo $carts[$i]["oid"];?>" onclick="selectone(this)"/>
						<input type="hidden" name="count_name_<?php echo $carts[$i]["oid"];?>" id="count_id_<?php echo $carts[$i]["oid"];?>"/>
						<input type="hidden" name="should_name_<?php echo $carts[$i]["oid"];?>" id="should_id_<?php echo $carts[$i]["oid"];?>"/>
					    <input type="hidden" name="database" id="left" value="<?php echo $A_product[0]["count"];?>"/>
					</td>
					<td>
						<a href=""><?php echo $A_product[0]["p_name"];?></a><div class="show_product">
							<img alt="good" style="width:50px;height:55px;"  src="<?php if(file_exists("../Image/product_img/".$A_product[0]["pid"].".jpg")){echo "../Image/product_img/".$A_product[0]["pid"].".jpg";}
										else {echo "../Image/product_img/item_none.png";}?>">
						</div>
					</td>
					<td>
						<p><?php echo number_format($A_product[0]["act_price"]*0.01 ,1, '.', '');?></p>
					</td>
					<td>
						<p id="product_price_<?php echo $carts[$i]["oid"];?>"><?php echo number_format($A_product[0]["act_price"] ,2, '.', ''); ?></p>
					</td>
					<td>
						<a href="javascript:;" onclick="minuteNumber(<?php echo $carts[$i]["oid"];?>);">[-]</a>
						<p id="product_book_<?php echo $carts[$i]["oid"];?>"><?php echo $carts[$i]["count"];?></p>
						<a href="javascript:;" onclick="addNumber(<?php echo $carts[$i]["oid"];?>);">[+]</a>
					</td>
					<td>
						<p id="product_discount_<?php echo $carts[$i]["oid"];?>"><?php $aaa=number_format($carts[$i]["count"]*$A_product[0]["act_price"]*(1-$discount_per_level*$_SESSION["User"]["level"]),1, '.', '');echo number_format(($carts[$i]["count"]*$A_product[0]["act_price"]-$aaa),1, '.', '');?>0</p>
					</td>
					<td>
						<p id="product_should_<?php echo $carts[$i]["oid"];?>"><?php echo $aaa;?>0</p>
					</td>
					<td>
						<a href="#" onclick="deleteone(<?php echo $carts[$i]["oid"];?>)">cancel</a>
					</td>
				</tr>
				<?php 
				$i++;
				
				if(!isset($carts[$i]))
				break;
				
				$A_product=$db->get_pid_product($carts[$i]["pid"]);?>
				<tr class="tr2">
					<td>
					<input type="checkbox" id="checkbox_<?php echo $carts[$i]["oid"];?>" name="name_check_<?php echo $carts[$i]["oid"];?>" onclick="selectone(this)"/>
					<input type="hidden" name="count_name_<?php echo $carts[$i]["oid"];?>" id="count_id_<?php echo $carts[$i]["oid"];?>"/>
					<input type="hidden" name="should_name_<?php echo $carts[$i]["oid"];?>" id="should_id_<?php echo $carts[$i]["oid"];?>"/>
					</td>
					<td>
						<a href=""><?php echo $A_product[0]["p_name"];?></a><div class="show_product">
							<img alt="good"  style="width:50px;height:55px;" src="<?php if(file_exists("../Image/product_img/".$A_product[0]["pid"].".jpg")){echo "../Image/product_img/".$A_product[0]["pid"].".jpg";}
										else {echo "../Image/product_img/item_none.png";}?>">
						</div>
					</td>
					<td>
						<p><?php echo number_format($A_product[0]["act_price"]*0.01,1, '.', '');?></p>
					</td>
					<td>
						<p id="product_price_<?php echo $carts[$i]["oid"];?>"><?php echo number_format($A_product[0]["act_price"],2, '.', '');?></p>
					</td>
					<td>
						<a href="javascript:;" onclick="minuteNumber(<?php echo $carts[$i]["oid"];?>);">[-]</a>
						<p id="product_book_<?php echo $carts[$i]["oid"];?>"><?php echo $carts[$i]["count"];?></p>
						<a href="javascript:;" onclick="addNumber(<?php echo $carts[$i]["oid"];?>);">[+]</a>
					</td>
					<td>
						<p id="product_discount_<?php echo $carts[$i]["oid"];?>"><?php $aaa=number_format($carts[$i]["count"]*$A_product[0]["act_price"]*(1-$discount_per_level*$_SESSION["User"]["level"]),1, '.', '');echo number_format(($carts[$i]["count"]*$A_product[0]["act_price"]-$aaa),1, '.', '');?>0</p>
					</td>
					<td>
						<p id="product_should_<?php echo $carts[$i]["oid"];?>"><?php echo $aaa;?>0</p>
					</td>
					<td>
						<a href="#" onclick="deleteone(<?php echo $carts[$i]["oid"];?>)">cancel</a>
					</td>
				</tr>
				<?php 
				$i++;}
				?>
				
			</table>
			<input type="submit" id="delete"  value="" onclick="deletecart(this);"/>
			
			<div id="price">
				<div>
					<p>Total：</p>
					<span class="money">$</span>
					<p class="money" id="total">0.00</p>
					<script language="JavaScript">
					total();
					</script>
				
				</div>
				<input type="button" id="ok" value="" onclick="paycart();"/>
				<input type="submit" id="save" value="" onclick="savecart();">
			</div>
		</form>
		<!-- <input type="button" id="decorate" value="Try to Decorate" onclick="window.location.href='design.php';"/> -->
	</div>
	<?php 
	}
	?>
</div>
<?php
include_once 'common/tail.php';
?>